<?php
/**
 * Resellerpanel's products class
 *
 * @access		public
 * @author		Svilen Spasov <svilen@svilen.com>
 * @package		AMBilling System
 *
 * @since			22-03-2006
 * @version		1.9
 */
class ResellerProducts {
	/**
	 * Db Object
	 * @access   private
	 * @var 		 object
	 */
	var $anydb;
	
	/**
	 * error handler
	 * @access   private
	 * @var 		 object
	 */
	var $error;
	
	/**
	 * Period
	 * @access   public
	 * @var 		 string
	 */
	var $period;
	
	/**
	 * Currency
	 * @access   public
	 * @var 		 string
	 */
	var $currency;
	
	/**
	 * Store products
	 * @access   public
	 * @var 		 array
	 */
	var $storeProducts;
	
	/**
	 * Product
	 * @access   public
	 * @var 		 array
	 */
	var $product = array();
	
	/**
	 * Constructor defines basic objects
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @param 	 object		$db						AnyDb object
	 *
	 * @see 		 Error::addError()
	 *
	 * @since    22-03-2006
	 * @version  1.0
	 */
	function ResellerProducts($db, $period = 12, $currency = 'USD', $where = '') {
		$this->error = new Error();
		$this->anydb = $db;
		// set currency
		$this->currency = $currency;
		// set period
		$this->period = $period;
		// get store products
		$this->getProducts($where);
		// set products' currency
		$this->setProductsCurrency($this->currency);
	}
	
	/**
	 * Get store products
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @see			 AnyDB::query()
	 * @see			 AnyDB::getAll()
	 * @see 		 Error::addError()
	 *
	 * @since    02-03-2006
	 * @version  1.0
	 */
	function getProducts ($where = '') {
		$recordSet = array();
		$productId = '';
				
		$query = "SELECT rp_product_id as product_id FROM rp_products $where";
		$this->anydb->query($query);
		
		// check for error
		if (!empty($this->anydb->error)) {
			$this->error->addError(3, $this->anydb->error);
			return FALSE;
		}
		
		$recordSet = $this->anydb->getAll();
		foreach ($recordSet as $key => $value) {
			$productId = $value['product_id'];
			$this->product[$productId] = new Product(&$this->anydb, &$this->error, $productId, $this->period, $this->currency, 'rp_products', 'rp_product_id');
		}
	}
	
	/**
	 * Get store products
	 *
	 * @access   public
	 * @author   Svilen Spasov <svilen@svilen.com>
	 *
	 * @since    22-03-2006
	 * @version  1.0
	 */
	function filterProductsForDnsCluster () {
		$recordSet = array();
		$productId = '';
		$dnsPackages = array();
		$resRegDomain = array();
		
		foreach ($this->product as $key => $value) {
			if ($value->type == 'dns_package') {
				$dnsPackages[$key] = $value;
			}
			
			if ($value->type == 'res_regdomain') {
				$resRegDomain[$key] = $value;
			}
		}
		
		// sort by prices
		$dnsPackages = ResellerProducts::sortProductsByPrice($dnsPackages);
		$resRegDomain = ResellerProducts::sortProductsByPrice($resRegDomain);
		
		$this->product = $dnsPackages + $resRegDomain;
	}
	
	/**
		* Sets detauls currency to all products objects
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @param		string		$currency			Currency string
		*
		* @return		bool			TRUE/FALSE
		*
		* @since    02-03-2006
		* @version  1.0
		*
		*/
	function setProductsCurrency ($currency = 'USD') {
		if (empty($currency)) {
			return FALSE;	
		}
		
		foreach ($this->product as $key => $value) {
			$this->product[$key]->setPriceCurrency($currency);
		}
				
		return TRUE;
	}
	
	/**
		* Sets default period to all products objects
		*
		* @access   public
		* @author   Svilen <svilen@svilen.com>
		*
		* @param		int				$period			Period value
		*
		* @return		bool			TRUE/FALSE
		*
		* @since    02-03-2006
		* @version  1.0
		*
		*/
	function setProductsPeriod ($period = 12) {
		if (empty($period)) {
			return FALSE;	
		}
		
		foreach ($this->product as $key => $value) {
			$this->product[$key]->setPricePeriod($period);
		}
		
		return TRUE;
	}
	
	/**
		* Sort all products in the internal product array
		*
		* @access   public
		* @author   Svilen  Spasov<svilen@svilen.com>
		*
		* @since    07-03-2006
		* @version  2.0
		*/
	function sortProducts () {
		$this->product = ResellerProducts::sortProductsByPrice($this->product);
	}
	
	/**
		* Sort all products in the internal products array
		*
		* @access   public
		* @author   Svilen  Spasov<svilen@svilen.com>
		*
		* @param 		array		$products			Products array
		* 
		* @return		array		Sorted products by price
		* 
		* @since    22-03-2006
		* @version  1.0
		*/
	function sortProductsByPrice ($products = array()) {
		usort ($products, array ('ResellerProducts', 'compareProducts'));
		
		// usort removes our keys from $products so we have to assign them again
		foreach ($products as $key => $value) {
			$products[$value->productId] = $value;
			unset($products[$key]);
		}
		
		return $products;
	}
	
	/**
		* Internal method for sorting products
		*
		* @access   private
		* @author   Svilen  Spasov <svilen@svilen.com>
		*
		* @param		object				$a			First object
		* @param		object				$b			Second object
		*
		* @return		int				0/-1/1
		*
		* @since    07-03-2006
		* @version  1.0
		*/
	function compareProducts ($a, $b) {
		if(!empty($a->seq) && !empty($b->seq)) {
			if ($a->seq > $b->seq) {
				return 1;
			}
			elseif ($a->seq < $b->seq) {
				return -1;
			}
			else {
				return 0;
			}
		}
		else if($a->wholesale->get() > $b->wholesale->get()) {
			return 1;
		}
		else if($a->wholesale->get() < $b->wholesale->get()) {
			return -1;
		}
		else {
			return 0;
		}
	}
}
?>